package Datastruct.Tree;

import com.sun.org.apache.xpath.internal.objects.XString;

public class Node {

    private int no;
    private String name;
    private Node left;
    private Node right;

    /**
     *
     */
    public Node(int no, String name) {
        this.name = name;
        this.no = no;
    }

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Node getLeft() {
        return left;
    }

    public void setLeft(Node left) {
        this.left = left;
    }

    public Node getRight() {
        return right;
    }

    public void setRight(Node right) {
        this.right = right;
    }

    @Override
    public String toString() {
        return "Node{" +
                "no=" + no +
                ", name='" + name + '\'' +
                ", left=" + left +
                ", right=" + right +
                '}';
    }

    /**
     * 前序遍历
     */
    public void preOrder() {
        //输出父结点
        System.out.println(this);
        if (this.left != null) {
            this.left.preOrder();
        }
        if (this.right != null) {
            this.right.preOrder();
        }
    }

    /**
     * 中序遍历
     */
    public void inOrder() {
        //左节点   父结点  右结点
        if (this.left != null) {
            this.left.inOrder();
        }
        System.out.println(this);

        if (this.right != null) {
            this.right.inOrder();
        }
    }

    /**
     * 后序遍历
     */
    public void postInOrder() {
        if (this.left != null) {
            this.left.postInOrder();
        }
        if (this.right != null) {
            this.right.postInOrder();
        }
        System.out.println(this);
    }


    //前序遍历查找
    public Node preSearch(int no) {

        if (this.no == no) {
            return this;
        }
        //查询左子结点
        Node node = null;
        if (this.left != null) {
            node = this.left.preSearch(no);

        }
        if (node != null) {
            return node;
        }
        if (this.right != null) {
            node = this.right.preSearch(no);
        }

        return node;

    }

    //中序遍历查找
    public Node inOrderSearch(int no) {
        Node node = null;
        if (this.left != null) {
            node = this.left.inOrderSearch(no);
        }
        if (node != null) {
            return node;
        }

        if (this.no == no) {
            return this;
        }
        if (this.right != null) {
            node = this.right.inOrderSearch(no);
        }
        return node;
    }

    //后序遍历查找
    public Node postOrdeSearch(int no) {
        Node node = null;
        if (this.left != null) {
            node = this.left.postOrdeSearch(no);
        }
        if (node != null) {
            return node;
        }
        if (this.right != null) {
            node = this.right.postOrdeSearch(no);
        }
        if (node != null) {
            return node;
        }
        if (this.no == no) {
            return this;
        }
        return node;
    }

    /*
     * 1.删除叶子结点
     * 2.删除的是子树，非叶子结点
     * */
    public void delNode(int no) {
        /**
         * 1.当前结点左节点不为空，并且左结点就是要删除的结点，this.left=null
         * 2.当前结点右节点不为空，并且右结点就是要删除的结点，this.right=null
         * 3.如果1,2,没有执行，那么需要向左子树进行递归删除
         * 4.如果3没有执行，则向右子树进行递归删除
         *
         * */
        if (this.left != null && this.left.no == no) {
            this.left = null;
            return;
        }

        if (this.right != null && this.right.no == no) {
            this.right = null;
            return;
        }

        //向左子树进行递归删除
        if (this.left != null) {
            this.left.delNode(no);
        }

        //向右子树进行递归删除
        if (this.right != null) {
            this.right.delNode(no);
        }
    }
}
